package com.symc.sims0213xfw.service.impl;

import com.symc.sims0213xfw.bean.College;
import com.symc.sims0213xfw.dao.CollegeDao;
import com.symc.sims0213xfw.dao.impl.CollegeDaoImpl;
import com.symc.sims0213xfw.service.CollegeService;
import com.symc.sims0213xfw.service.OperationStatusResult;

import java.util.List;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/26 13:51
 * @Description:
 */
public class CollegeServiceImpl implements CollegeService {
    private CollegeDao collegeDao = new CollegeDaoImpl();

    @Override
    public List<College> getOptionalCollegeList() {
        return new CollegeDaoImpl().queryListVisibleCollege();
    }

    @Override
    public Long addCollege(Integer id, String name) {
        if (id > College.ID_MAX || id < College.ID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (name.length() > College.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (collegeDao.queryVisibleCollege(id) != null) {//用户可见
            return OperationStatusResult.EXIST;
        } else if (collegeDao.queryCollege(id) != null) {//库中有,但不可见
            if (collegeDao.updateCollege(id, name) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        } else {//库中没有
            if (collegeDao.addCollege(id, name) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long updateCollege(Integer id, String name) {
        if (id > College.ID_MAX || id < College.ID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (name.length() > College.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (collegeDao.queryVisibleCollege(id) == null) {//用户可见
            return OperationStatusResult.NOT_EXIST;
        } else {
            if (collegeDao.updateCollege(id, name) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long deleteCollege(Integer id) {
        if (collegeDao.queryVisibleCollege(id) == null) {
            return OperationStatusResult.NOT_EXIST;//ok
        } else {
            if (collegeDao.deleteVisibleCollege(id) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }
}
